"""
@File    :   database.py
@Time    :   2024/03/01 11:05:14
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   全局数据库连接相关文件
"""

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker

from src.config import get_app_settings

# 获取全局配置
settings = get_app_settings()

# 主库相关连接
# 创建异步引擎对象
async_engine = create_async_engine(
    settings.master_async_orm_url,
    echo=settings.orm_echo,
    future=settings.orm_future,
    pool_size=settings.orm_process_pool_size,
    pool_pre_ping=settings.orm_pool_pre_ping,
)


# 创建异步会话管理对象(连接池对象, 每次调用就会取出一个连接)
masterSessionLocal = sessionmaker(
    async_engine,
    class_=AsyncSession,
    autocommit=settings.orm_auto_commit,
    autoflush=settings.orm_auto_flush,
    expire_on_commit=settings.orm_expire_on_commit,
)
